package PartEdgeDemo;

import java.util.ArrayList;
import java.util.HashMap;

import y.base.Edge;
import y.base.EdgeCursor;
import y.base.Node;
import y.view.Graph2D;

public class DegreeCalc {
 
	private Graph2D g;

	// Klasse um Winkel zwischen Kanten zu berechnen.
	// Noch keine usability, kann aber dazu verwendet werden Graphen lesbarer zu machen.
	public DegreeCalc(Graph2D g) {
		this.g = g;
	}
	
	public void shackle() {
		
		

	}

	public HashMap<Node, ArrayList<Edge>> degreeCalc() { 

		Node[] nodeArr = g.getNodeArray();
		ArrayList<Edge> lowDegreeEdges = new ArrayList<Edge>();
		HashMap<Node, ArrayList<Edge>> lowDegreeMap = new HashMap<Node, ArrayList<Edge>>();
		Node v;
		EdgeCursor ec;
		Edge e1;
		Node node2;
		
		for (int i = 0; i < nodeArr.length; i++) {
			v = nodeArr[i];
			int deg = v.degree();// anz der Kanten im Knoten
			if (deg > 1) {
				ec = v.edges();
				e1 = (Edge) ec.current();
				node2 = e1.opposite(v);
				double x1 = g.getCenterX(v);
				double y1 = g.getCenterY(v);
				double x2 = g.getCenterX(node2);
				double y2 = g.getCenterY(node2);

				double deltaX = x1 - x2;
				double deltaY = y1 - y2; // 


				for (int j = 0; j < ec.size() - 1; j++) {
					ec.cyclicNext();
					Edge e2 = (Edge) ec.current();
					Node node3 = e2.opposite(v);
					
					double x3 = g.getCenterX(node3);
					double y3 = g.getCenterY(node3);
					
					        
					
					
					double deltaXx = x1 - x3;
					double deltaYy = y1 - y3;
					
					double angle1 = Math.atan2(deltaY,
							deltaX);
					double angle2 = Math.atan2(deltaYy,
							deltaXx);
					double erg = Math.toDegrees(angle1-angle2);					
					if (erg < 1 && erg > -1) {
						lowDegreeEdges.add(e2);
						lowDegreeEdges.add(e1);
					}
				}
			}
			if (lowDegreeEdges.size() > 0)
				lowDegreeMap.put(v, lowDegreeEdges);
			lowDegreeEdges = new ArrayList<Edge>();
		}
		System.out.println(lowDegreeMap);
		return lowDegreeMap;
	}

}
